#coding=utf-8
from urllib import request
import bs4
from bs4 import Tag
from flask import jsonify


def parse(url):

    # 爬取结果
    response = request.urlopen(url)

    data = response.read()

    # 设置解码方式
    data = data.decode('utf-8')

    # 打印结果
    return str(data)


def fetch_article_list(user):

    url = 'https://blog.csdn.net/' + user
    html = parse(url)
    soup = bs4.BeautifulSoup(html, 'html.parser')

    article_divs = soup.find('div', 'article-list')

    articles = list()
    for item in article_divs.children:
        try:
            if str(item) != '\n':
                map = dict()
                articles.append(map)
                title = item.find('h4', 'text-truncate').find('a')
                title_str = ''
                for title_child in title.children:
                    if type(title_child) != Tag:
                        temp = title_child.strip('\n').strip()
                        if len(temp) > 1:
                            title_str = temp
                content = item.find('p', 'content').find('a')
                content_str = (content.text.strip('\n').strip())
                map.setdefault('title', title_str)
                    # map.pop('title', title_str)
                map.setdefault('content', content_str)
        except Exception as e:
            print("解析失败:")
            print(item)
            print(e)
    return articles


def fetch_article(url):


    return None

